﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Cartoon.Model
{
    public class ChapterList:List<Chapter>,IExport
    {
        public string CartoonName {get;set;}
        public string Url { get; set; }
        public string ComicID { get; set; } 
        public int MaxChapterID { get; set; }
        public ChapterList(string _cartoonName,string _url,string _comicId)
        {
            this.CartoonName = _cartoonName;
            this.Url = _url;
            this.ComicID = _comicId;
        }

        

        public void Sort(bool desc)
        {
            if (desc == true)
            {
                this.Sort(new Comparison<Chapter>((c1, c2) => { return c2.Id - c1.Id; }));
            }
            else
            {
                this.Sort(new Comparison<Chapter>((c1, c2) => { return c1.Id - c2.Id; }));
            } 
        }

        public DataTable ToDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ComicId");
            dt.Columns.Add("Id");
            dt.Columns.Add("Src");
            dt.Columns.Add("PicListUrl", new object().GetType());
            dt.Columns.Add("PicCount");
            dt.Columns.Add("PicAllSize");
            dt.Columns.Add("CartoonName");
            dt.Columns.Add("Url");

            foreach (Chapter c in this)
            {
                DataRow newRow = dt.NewRow();
                newRow["ComicId"] = c.comic.Id;
                newRow["Id"] = c.Id;
                newRow["Src"] = c.Src;
                newRow["PicListUrl"] = c.PicListUrl!=null? String.Join("|", c.PicListUrl.ToArray()):"";
                newRow["PicCount"] = c.PicCount;
                newRow["PicAllSize"] = c.PicAllSize;
                newRow["CartoonName"] = this.CartoonName;
                newRow["Url"] = this.Url;

                dt.Rows.Add(newRow);
            }
            

            dt.TableName = "《" + this.CartoonName + "》";

            return dt;
            
        }

        public void Save(string filename)
        {
            FileInfo fileInfo = new FileInfo(filename);
            if (fileInfo.Exists == false)
            {
                Directory.CreateDirectory( fileInfo.DirectoryName);
 
                using (FileStream fs = File.Create(fileInfo.FullName))
                {
                    fs.Close();
                }

            }

            DataSet ds = new DataSet();
            ds.Tables.Add(this.ToDataTable());
            ds.WriteXml(filename);
        }

        public static ChapterList Load(string filepath,Comic c)
        {
            ChapterList cl = new ChapterList(c.Name, c.Url, c.Id.ToString());
            cl.MaxChapterID = 0;

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            dt.TableName = "《" + cl.CartoonName + "》";

            try
            {
                ds.ReadXml(filepath);
                dt = ds.Tables[0];
            }
            catch (Exception ignore) { return cl; } 

            if (dt == null || dt.Rows.Count == 0) return null;
            
            foreach (DataRow row in dt.Rows)
            {
                Chapter chp = new Chapter()
                        {
                            comic = c,
                            Id = Convert.ToInt32(row["Id"]),
                            Src = row["Src"].ToString(),
                            PicListUrl = row["PicListUrl"].ToString().Split('|').ToList(),
                            PicCount = Convert.ToInt32(row["PicCount"]),
                            PicAllSize = Convert.ToInt64(row["PicAllSize"])

                        };

                if (chp.Id > cl.MaxChapterID) cl.MaxChapterID = chp.Id;

                cl.Add(chp);
            }
            return cl;
        }


        public void UpdateChapter(Chapter chp)
        { 
             
        }

    }
}
